/*
 * @lc app=leetcode.cn id=594 lang=javascript
 *
 * [594] 最长和谐子序列
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var findLHS = function(nums) {
    let map = new Map()
    for (let index = 0; index < nums.length; index++) {
        const element = nums[index];
        let value = map.get(element)
        if (!value) {
            map.set(element, 1)
        } else {
            map.set(element, value + 1)
        }
    }
    let keys = [...map.keys()].sort((a, b) => a - b)
    let arr = []
    for (let index = 1; index < keys.length; index++) {
        const element = keys[index];
        const pre = keys[index - 1]
        if (element - pre === 1) {
            arr.push(map.get(element) + map.get(pre))
        }
    }
    return Math.max(...arr, 0)
};
// @lc code=end

